//
//  YTHomeMainCell.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/10.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHomeMainCell.h"
#import "YTHomeBannerCell.h"
#import "YTHomeArticleCell.h"
#import "YTHomeMarqueeCell.h"
#import "YTHomeDetailController.h"
#import "UIImage+Category.h"
#import <MJRefresh/MJRefreshGifHeader.h>

NSString *homeBannerCellID = @"homeBannerCellID";
NSString *homeArticleCellID = @"homeArticleCellID";
NSString *homeMerCellID = @"homeMerCellID";

@interface YTHomeMainCell ()<UITableViewDataSource ,UITableViewDelegate>

@property(nonatomic ,strong)UITableView *tableView;

@end

@implementation YTHomeMainCell


- (void)setBanner:(YTHomeBanner *)banner
{
    _banner = banner;
    [self.tableView reloadData];
}

// 是否首页setter
- (void)setIsHotView:(BOOL)isHotView
{
    _isHotView = isHotView;
    
    [self.tableView reloadData];
}

- (void)setList:(NSArray<YTHomeArticle *> *)list
{
    _list = list;
    [self.tableView reloadData];
}

- (void)setFlash:(NSArray<YTNewFlashModel *> *)flash
{
    _flash = flash;
    
    [self.tableView reloadData];
}

// lazy tableview
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        // 注册可重用cell
        [_tableView registerClass:[YTHomeBannerCell class]
                    forCellReuseIdentifier:homeBannerCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"YTHomeArticleCell" bundle:nil]
                                            forCellReuseIdentifier:homeArticleCellID];
        

        [_tableView registerNib:[UINib nibWithNibName:@"YTHomeMarqueeCell" bundle:nil]
                                            forCellReuseIdentifier:homeMerCellID];
        
        // setupRefreshHeader RefreshFooter
        YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
            
            if ([self.loadDelegate respondsToSelector:@selector(loadDataWithTableView:pullup:idx:)]) {
                [self.loadDelegate loadDataWithTableView:self->_tableView pullup:NO idx:self.tag];
            }
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if ([self.loadDelegate respondsToSelector:@selector(loadDataWithTableView:pullup:idx:)]) {
                [self.loadDelegate loadDataWithTableView:self->_tableView pullup:YES idx:self.tag];
            }
        }];
        

        [footer setTitle:@"没有更多了哦～" forState:MJRefreshStateNoMoreData];
        [footer setTitle:@"" forState:MJRefreshStateIdle];

        
        _tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
        
        
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
    }
    return _tableView;
}


- (void)begainReflash
{
    if ([self.loadDelegate respondsToSelector:@selector(loadDataWithTableView:pullup:idx:)]) {
        [self.loadDelegate loadDataWithTableView:self.tableView pullup:NO idx:self.tag];
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       
        self.tableView.frame = self.bounds;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        //使有数据的cell 显示下划线
//        self.tableView.tableFooterView = [[UIView alloc]init];
//        //设置分割线内边距
//        self.tableView.separatorInset = UIEdgeInsetsZero;

        [self.contentView addSubview:self.tableView];
        
        //self.tableView.contentInset = UIEdgeInsetsMake(24.5, 0, 0, 0);
        
     
        
    }
    return self;
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
}

#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.banner ? self.list.count + 2 : self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID;
    
    if (self.isHotView) {
        switch (indexPath.row)
        {
            case 0:
                cellID = homeBannerCellID;
                break;
            case 1:
                cellID = homeMerCellID;
                break;
            default:
                cellID = homeArticleCellID;
                break;
        }
        
    }else{
        cellID = homeArticleCellID;
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    if (cellID == homeBannerCellID) {
        [(YTHomeBannerCell *)cell setBanner:self.banner];
        
    }
    
    if (cellID == homeMerCellID) {
        [(YTHomeMarqueeCell *)cell setFlashList:self.flash];
    }
    
    if (cellID == homeArticleCellID) {
        [(YTHomeArticleCell *)cell setArticle:self.banner ? self.list[indexPath.row - 2] : self.list[indexPath.row]];
    }
    
//    cell.layoutMargins = UIEdgeInsetsZero;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


#pragma mark: - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.isHotView ? ((indexPath.row == 0) ? (iPhone5 ? 150 : 200) : (indexPath.row == 1) ? (iPhone5 ? 70 : 80) : (iPhone5 ? 80 : 104)) : (iPhone5 ? 80 : 104);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row <= 1 && self.isHotView) {
        return;
    }
    YTHomeArticle *article = self.isHotView ? self.list[indexPath.row - 2] : self.list[indexPath.row];
    
    [self.loadDelegate showArticleDetail:self idx:indexPath.row article:article];
    
   
}


- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([cell isKindOfClass:[YTHomeMarqueeCell class]]) {
        [(YTHomeMarqueeCell *)cell cellDidEndDisplay];
    }
}


@end
